﻿using UnityEngine;
using System.Collections.Generic;

public class Boss03Controller : MonoBehaviour
{
    public GameObject[] ArrayBullet;
    public GameObject Head;
    public float[] ArraySpeed;
    public float[] ArraySpeedHead;
    public float lowHealth;


    private List<Vector3> listVector;
    private List<Vector3> listVectorHead;
    private Step step;
    private float timeBullet;
    private float temp;
    private float tempHead;
    private float timeTemp;
    private int  countBullet;
    private float speedBullet;
    private float timeShootBullet;
    private bool checkStep05;
    private bool checkShoot;
    private int rangeAngle;
    private BulletController bulletController;
    private EnemyController enemyController;
    private PlayerController player;
    private float timedelay;
    private Vector3 P0,P1,P2,P3,P4;
    private float delay;
    private float delayHead;
    private Benzier benzier;

    void Awake()
    {
        benzier = new Benzier();
        listVector = new List<Vector3>();
        listVectorHead = new List<Vector3>();
        speedBullet = -5;
        countBullet = 5;
        temp = 0;
        tempHead = temp;
        delay = 0.003f;
        delayHead = 0.00302f;
       benzier.AddList(ref listVector,new Vector3(1.67f, 7), new Vector3(1.5f, 2.5f), new Vector3(-0.2f, 2.5f), new Vector3(-1.9f, 3.2f));
        step = Step.step01;
        
        bulletController = gameObject.GetComponent<BulletController>();
        enemyController = gameObject.GetComponent<EnemyController>();
        player = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerController>();
    }

   void FixedUpdate()
   {
       timeBullet += Time.deltaTime;
       switch (step)
       {
           case Step.step01:
               Shoot1(2.5f);
              rigidbody.position = benzier._Benzier(listVector, ref temp, delay);
              Head.rigidbody.position = benzier._Benzier(listVector, ref tempHead, delayHead);
              if (transform.position.x <= -1.6f)
               {
                   step = Step.step02;
                   timeBullet = 0.5f;
                   timeTemp = 1;
               }
               break;
           case Step.step02:
               Shoot2(ref  timeTemp);
               rigidbody.velocity = new Vector3(0, 1) * ArraySpeed[0];
              Head.rigidbody.velocity = new Vector3(0, 1) * ArraySpeedHead[0];
               if (transform.position.y <= 0.5f)
               {
                   step = Step.step03;
                   timeTemp = 1.55f;
                   timeBullet = -0.05f;
               }
               break;
           case Step.step03:
               Shoot5(timeTemp);
               rigidbody.velocity = new Vector3(-1, 0) * (ArraySpeed[0]);
               Head.rigidbody.velocity = new Vector3(-1, 0) * ArraySpeedHead[0];
               if (transform.position.x >= 1.6f)
               {
                   step = Step.step04;
                   timeBullet =0;
                   timeTemp = 1;
                   speedBullet = -5;
                   countBullet = 5;
               }
               break;
           case Step.step04:
               Shoot2(ref timeTemp);
               rigidbody.velocity = new Vector3(0, -1) * (ArraySpeed[0]);
               Head.rigidbody.velocity = new Vector3(0, -1) * ArraySpeedHead[0];
               if (transform.position.y >= 3)
               {
                   step = Step.step05;
                   timeBullet = -0.65f;
                   timeTemp = 1;
               }
               break;
           case Step.step05:
               Shoot5(timeTemp);
              
               rigidbody.velocity = new Vector3(1, 0) * (ArraySpeed[0]);
               Head.rigidbody.velocity = new Vector3(1, 0) * ArraySpeedHead[0];
               if (checkStep05)
               {
                   step = Step.step06;
                  benzier.AddList(ref listVector,rigidbody.position, new Vector3(0.2f, 4), new Vector3(-1.7f, 3));
                  benzier.AddList(ref listVectorHead,Head.rigidbody.position, new Vector3(0.2f, 4), new Vector3(-1.7f, 3));
                   temp = 0;
                   tempHead = 0;
                   delay = 0.006f;
                   delayHead = 0.00604f;
                   checkStep05 = false;
                   timeTemp = 0.7f;
                   timeBullet = 0;
               }
               if (transform.position.x <= -1.6f && !checkStep05)
               {
                   step = Step.step02;
                   timeBullet = 0;
                   timeTemp = 1;
                   speedBullet = -5;
                   countBullet = 5;
                   checkStep05 = true;
               }
               break;
           case Step.step06:
               Shoot6(timeTemp);
               rigidbody.position = benzier._Benzier(listVector, ref temp, delay);
              Head. rigidbody.position = benzier._Benzier( listVectorHead,ref tempHead, delayHead);
               if (transform.position.x <= -1.6f)
               {
                   step = Step.step07;
                   benzier.AddList(ref listVector,rigidbody.position, new Vector3(-1.1f, 1f), new Vector3(-0.31f, 2));

                   benzier.AddList(ref listVectorHead,Head.rigidbody.position, new Vector3(-1.1f, 1f), new Vector3(-0.31f, 2));
                   temp=0;
                   tempHead = temp;
                   timeBullet = -0.5f;
               }
               break;
           case Step.step07:
               Shoot6(timeTemp);
                rigidbody.position = benzier._Benzier(listVector, ref temp, delay);
             Head.rigidbody.position = benzier._Benzier(listVectorHead, ref tempHead, delayHead);
                if (transform.position.x >= -0.4f)
                {
                    step = Step.step08;
                    temp = 0;
                    tempHead = temp;
                    benzier.AddList(ref listVector, rigidbody.position, new Vector3(0.84f, 3f), new Vector3(1.6f, 2));
                    benzier.AddList(ref listVectorHead,Head.rigidbody.position, new Vector3(0.84f, 3f), new Vector3(1.6f, 2));
                    timeBullet = -0.5f;
                }
               break;

           case Step.step08:
               Shoot6(timeTemp);
               rigidbody.position = benzier._Benzier(listVector, ref temp, delay);
               Head.rigidbody.position = benzier._Benzier(listVectorHead, ref tempHead, delayHead);
               if (transform.position.x >= 1.5f)
               {
                   step = Step.step09;
                   temp = 0;
                   tempHead = temp;
                   timeBullet = 0;
                   benzier.AddList(ref listVector, rigidbody.position, new Vector3(1.2f, 0.5f), new Vector3(-0.2f, 0.5f), new Vector3(-1.64f, 2.5f));
                   benzier.AddList(ref listVectorHead, Head.rigidbody.position, new Vector3(1.2f, 0.5f), new Vector3(-0.2f, 0.5f), new Vector3(-1.64f, 2.5f));
               }
               break;
           case Step.step09:
               Shoot6(timeTemp);
               rigidbody.position = benzier._Benzier(listVector, ref temp, delay);
               Head.rigidbody.position = benzier._Benzier(listVectorHead, ref tempHead, delayHead);
               if (transform.position.x <= -1.5f)
               {
                   step = Step.step10;
                   temp = 0;
                   tempHead = temp;
                   delay = 0.002f;
                   delayHead = 0.00201f;
                   timeTemp = 0.5f;
                   timeBullet = 0;
                   benzier.AddList(ref listVector, rigidbody.position, new Vector3(-1.5f, 3), new Vector3(-0.5f, 2f));
                   benzier.AddList(ref listVectorHead,Head.rigidbody.position, new Vector3(-1.5f, 3), new Vector3(-0.5f, 2f));
               }
               break;
           case Step.step10:
               Shoot7(timeTemp);
               rigidbody.position = benzier._Benzier(listVector, ref temp, delay);
               Head.rigidbody.position = benzier._Benzier(listVectorHead, ref tempHead, delayHead);
               timedelay += Time.deltaTime;
               if (timedelay >= 2)
               {
                   step = Step.step11;
                   timeBullet = 0;
                   timeTemp=0;
               }

               break;
           case Step.step11:
               Shoot3(ref timeTemp,0.03f);
              rigidbody.velocity = new Vector3(-1,-0.2f)*ArraySpeed[1];
              Head.rigidbody.velocity = new Vector3(-1, -0.2f) * ArraySpeedHead[1];
               if (transform.position.x >= 1.4f)
               {
                   step = Step.step12;
                   temp = 0;
                   tempHead = temp;
                   delay = 0.003f;
                   delayHead = 0.00301f;
                   timeBullet = 0;
                   timeTemp = 0.5f;
                   timedelay = 0;
                   benzier.AddList(ref listVector,rigidbody.position, new Vector3(1.5f, 2.5f), new Vector3(0.5f, 3));
                   benzier.AddList(ref listVectorHead,Head.rigidbody.position, new Vector3(1.5f, 2.5f), new Vector3(0.5f, 3));
               }

               break;

           case Step.step12:
               Shoot7(timeTemp);
               rigidbody.position = benzier._Benzier(listVector, ref temp, delay);
               Head.rigidbody.position = benzier._Benzier(listVectorHead, ref tempHead, delayHead);
               timedelay += Time.deltaTime;
               if (timedelay >= 2)
               {
                   step = Step.step13;
                   timeBullet = 0;
                   timeTemp = 0;
                  
               }
               break;
           case Step.step13:
               Shoot3(ref timeTemp, 0.03f);
               rigidbody.velocity = new Vector3(1, -0.2f) * ArraySpeed[1];
               Head.rigidbody.velocity = new Vector3(1, -0.2f) * ArraySpeedHead[1];
               if (transform.position.x <= -1.3f)
               {
                   step = Step.step14;
                   temp = 0;
                   tempHead = temp;
                   timedelay = 0;
                   timeBullet = 0;
                   timeTemp = 0.5f;
                   benzier.AddList(ref listVector,rigidbody.position, new Vector3(-0.8f, 2.5f), new Vector3(-0.5f, 3));
                   benzier.AddList(ref listVectorHead, Head.rigidbody.position, new Vector3(-0.8f, 2.5f), new Vector3(-0.5f, 3));
               }
               break;
           case Step.step14:
               Shoot7(timeTemp);
               rigidbody.position = benzier._Benzier(listVector, ref temp, delay);
               Head.rigidbody.position = benzier._Benzier(listVectorHead, ref tempHead, delayHead);
               timedelay += Time.deltaTime;
               if (timedelay >= 2)
               {
                   step = Step.step15;
                   timeTemp = 0;
                   timeBullet = 0;

               }
               break;
           case Step.step15:
               Shoot3(ref timeTemp, 0.03f);
               rigidbody.velocity = new Vector3(-1, 0.5f) * ArraySpeed[1];
               Head.rigidbody.velocity = new Vector3(-1, 0.5f) * ArraySpeedHead[1];
               if (transform.position.x >= 1.4f)
               {
                   step = Step.step16;
                   temp = 0;
                   tempHead = temp;
                   timedelay = 0;
                   timeTemp = 0.5f;
                   timeBullet = 0;
                   benzier.AddList(ref listVector, rigidbody.position, new Vector3(1.1f, 2.3f), new Vector3(0.5f, 1.2f));
                   benzier.AddList(ref listVectorHead, Head.rigidbody.position, new Vector3(1.1f, 2.3f), new Vector3(0.5f, 1.2f));
               }
               break;
           case Step.step16:
               Shoot7(timeTemp);
              rigidbody.position = benzier._Benzier(listVector, ref temp, delay);
              Head.rigidbody.position = benzier._Benzier(listVectorHead, ref tempHead, delayHead);
               timedelay += Time.deltaTime;
               if (timedelay >= 2)
               {
                   step = Step.step17;
                   timeBullet = 0;
                   timeTemp = 0;

               }
               break;
           case Step.step17:
               Shoot3(ref timeTemp, 0.03f);
               rigidbody.velocity = new Vector3(1, 0) * ArraySpeed[1];
               Head.rigidbody.velocity = new Vector3(1, 0) * ArraySpeedHead[1];
               if (transform.position.x <= -1.4f)
               {
                   step = Step.step18;
                   temp = 0;
                   tempHead = temp;
                   timedelay = 0;
                   timeTemp = 0.5f;
                   timeBullet = 0;
                   benzier.AddList(ref listVector, rigidbody.position, new Vector3(-1.5f, 0.75f), new Vector3(-1, 2));
                   benzier.AddList(ref listVectorHead, Head.rigidbody.position, new Vector3(-1.5f, 0.75f), new Vector3(-1, 2));
               }
               break;
           case Step.step18:
               Shoot7(timeTemp);
              rigidbody.position = benzier._Benzier(listVector, ref temp, delay);
              Head.rigidbody.position = benzier._Benzier(listVectorHead, ref tempHead, delayHead);
               timedelay += Time.deltaTime;
               if (timedelay >= 2)
               {
                   step = Step.step19;
               }
               break;
           case Step.step19:
               rigidbody.velocity = new Vector3(-1,- 0.6f) *ArraySpeed[1];
               Head.rigidbody.velocity = new Vector3(-1,- 0.6f) * ArraySpeedHead[1];
               if (transform.position.x >= 1.4f)
               {
                   step = Step.step20;
                   timeTemp = 0.6f;
                   timeBullet = 0.5f;
               }
               break;
           case Step.step20:
               Shoot3(ref timeTemp, 0.1f);
               rigidbody.velocity = new Vector3(0.1f,1)*ArraySpeed[2];
               Head.rigidbody.velocity = new Vector3(0.1f, 1) * ArraySpeedHead[2];

               if (transform.position.y <= 0.5f)
               {
                   step = Step.step21;
                   benzier.AddList(ref listVector, rigidbody.position, new Vector3(0.9f, -0.5f), new Vector3(0.3f, 1.2f));
                   benzier.AddList(ref listVectorHead, Head.rigidbody.position, new Vector3(0.9f, -0.5f), new Vector3(0.3f, 1.2f));
                   temp = 0;
                   tempHead = temp;
                   delay = 0.006f;
                   delayHead = 0.00602f;
                   
               }
               break;
           case Step.step21:
               Shoot3(ref timeTemp, 0.1f);
               rigidbody.position = benzier._Benzier(listVector, ref temp, delay);
               Head.rigidbody.position = benzier._Benzier(listVectorHead, ref tempHead, delayHead);
               if (transform.position.x <= 0.4f)
               {
                   step = Step.step22;
               }

               break;
           case Step.step22:
               Shoot3(ref timeTemp, 0.1f);
               rigidbody.velocity = new Vector3(0.3f, -1) * ArraySpeed[2];
              Head.rigidbody.velocity = new Vector3(0.3f, -1) * ArraySpeedHead[2];
               if (transform.position.y >= 3)
               {
                   step = Step.step23;
                   temp = 0;
                   tempHead = temp;
                   delay = 0.004f;
                   delayHead = 0.00402f;
                   benzier.AddList(ref listVector, rigidbody.position, new Vector3(-0.75f, 5), new Vector3(-2.8f, 0.5f));
                   benzier.AddList(ref listVectorHead, Head.rigidbody.position, new Vector3(-0.75f, 5), new Vector3(-2.8f, 0.5f));
               }
               break;
           case Step.step23:
               Shoot3(ref timeTemp, 0.1f);
               rigidbody.position = benzier._Benzier(listVector, ref temp, delay);
               Head.rigidbody.position = benzier._Benzier(listVectorHead, ref tempHead, delayHead);
               if (transform.position.x <= -1.6f)
               {
                   step = Step.step02;
                   timeTemp = 1;
                   timeBullet = 0.05f;
                 
               }
               break;
       }

   }




    void ShootFindTarget( GameObject Bullet, float speed,float range,int numberBullet,float rangeangle)
    {
        Quaternion rotation = Quaternion.LookRotation(player.transform.position- Head.transform.position);
        float angle=90 ;

        int temp = (int)rotation.eulerAngles.y ;
        if (temp <= 90)
        {
            angle = 90 + (90 - rotation.eulerAngles.x);
        }
        else if (temp == 270)
        {
            angle = 90 - (90 - rotation.eulerAngles.x);
        }

        bulletController.Bullet_Forward(Bullet, numberBullet, rangeangle, angle - range, speed, 1, enemyController.status.Damage);
    }

    void Shoot1(float time)
    {
        timeShootBullet = time;
        if (timeBullet >= timeShootBullet)
        {

            timeShootBullet = timeBullet + 0.1f;
            if (timeShootBullet >= 0.2f)
            {
                bulletController.Bullet_Forward(ArrayBullet[1], 6, Random.Range(10, 15), 60, -3.5f, 1, enemyController.status.Damage);
                bulletController.Bullet_Forward(ArrayBullet[0], 6, Random.Range(10, 15), 60, -3, 1, enemyController.status.Damage);
                timeBullet = 0;
            }
        }
    }

    void Shoot2(ref float temp)
    {
        if (timeBullet >= temp)
        {
            ShootFindTarget(ArrayBullet[0], speedBullet, 5, countBullet, 7);
            temp += 0.05f;
            speedBullet += 0.5f;
            countBullet--;
            if (temp >= 1.4f)
            {
                timeBullet = -10;
            }

        }

    }

    void Shoot3(ref float time,float delay)
    {
        timeShootBullet = time;
        if (timeBullet >= timeShootBullet)
        {
            time = timeBullet + delay;
            ShootFindTarget(ArrayBullet[0], -5, 0, 2, 2.5f);
            if (time >= 1.4f)
            {
                ShootFindTarget(ArrayBullet[0], -5, -30, 2, 2.5f);
                ShootFindTarget(ArrayBullet[0], -5, 30, 2, 2.5f);
                ShootFindTarget(ArrayBullet[0], -4, -10, 2, 2.5f);
                ShootFindTarget(ArrayBullet[0], -4, 10, 2, 2.5f);
                timeBullet = -1;
                time = 0.6f;
            }
        }
    }

    void Shoot4()
    {

        if (timeBullet >= 0.15f)
        {
            timeBullet = 0;
            bulletController.Bullet_Forward(ArrayBullet[0], 1, 0, 60 + rangeAngle, -5f, 1, enemyController.status.Damage);
            bulletController.Bullet_Forward(ArrayBullet[0], 1, 0, 60 + rangeAngle, -4.7f, 1, enemyController.status.Damage);
            bulletController.Bullet_Forward(ArrayBullet[0], 1, 0, 240 + rangeAngle, -5f, 1, enemyController.status.Damage);
            bulletController.Bullet_Forward(ArrayBullet[0], 1, 0, 240 + rangeAngle, -4.7f, 1, enemyController.status.Damage);
            rangeAngle += 10;
        }
    }
    void Shoot5(float time)
    {
        if (timeBullet >= time)
        {
            for (int i = 1; i <= 12; i++)
                bulletController.Bullet_Forward(ArrayBullet[0], 2, 5,30 * i, -4f, 1, enemyController.status.Damage);
            timeBullet = 0;
        }
    }
    
    void Shoot6(float time)
    {
        if (timeBullet >= time)
        {
            ShootFindTarget(ArrayBullet[0], -4, 0, 3, 10);
            timeBullet = 0;
        }
    }
    void Shoot7(float time)
    {
        if (timeBullet >= time)
        {
            timeBullet = -5;
            bulletController.Bullet_Cricle(ArrayBullet[0], 16, 22.5f, 0, -4, 1, enemyController.status.Damage);
            bulletController.Bullet_Cricle(ArrayBullet[1], 10, 36, 0, -5, 1, enemyController.status.Damage);
        }
    }
   
}
